//
//  AreaListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/25.
//

#import "AreaListCell.h"

@interface AreaListCell()

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIImageView *tickImageView;

@end

@implementation AreaListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@22);
        make.right.mas_equalTo(@-50);
        make.height.mas_equalTo(@20);
        make.centerY.mas_equalTo(self_weak_.contentView);
    }];
    self.tickImageView = [[UIImageView alloc]init];
    self.tickImageView.image = [UIImage imageNamed:@"area_tick"];
    [self.contentView addSubview:self.tickImageView];
    [self.tickImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-22);
        make.width.height.mas_equalTo(@18);
        make.centerY.mas_equalTo(self_weak_.contentView);
    }];
    self.tickImageView.hidden = YES;
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@1);
        make.bottom.mas_equalTo(@0);
    }];
    
    
}

-(NSMutableAttributedString*)changeStr:(NSString*)str andStr2:(NSString*)str2{
    NSMutableAttributedString *pStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@",str,str2]];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontBlackColor range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontGrayColor range:NSMakeRange(str.length,pStr.length-str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontPingFangSCRegular(14) range:NSMakeRange(0,pStr.length)];
    return pStr;
}

-(void)setIsSel:(BOOL)isSel{
    _isSel = isSel;
    if(isSel == YES){
        self.tickImageView.hidden = NO;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(@-50);
        }];
    }else{
        self.tickImageView.hidden = YES;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(@-22);
        }];
    }
}

-(void)setModel:(AreaListModel *)model{
    _model = model;
    self.titleLabel.attributedText = [self changeStr:model.areaName andStr2:[NSString stringWithFormat:@"+%@",model.areaId]];
}


@end
